import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class HighScoreCanvas extends GameCanvas implements Runnable {
	
	private static final int PADDING = 2;
	private static final int BGR_COLOR = 0x3A79FF;
	
	public static ScoreRecord[] recs;
	private Display display;
	private MenuCanvas menuCanvas;
	
	private int width;
	private int height;

	private int totalHeight;
	private int itemHeight;
	
	private int startY;
	
	public boolean isBackToMenu = true;
	public BoardCanvas board;
	
	public int blinkItem = 100;
	private Thread thread;
	
	public HighScoreCanvas(Display display, MenuCanvas menuCanvas) {
		super(false);
		this.display = display;
		this.menuCanvas = menuCanvas;
		setFullScreenMode(true);
		width = getWidth();
		height = getHeight();
		
		recs = MyIO.loadHighScore();
		
		itemHeight = ThemeManager.Small.pieceHeight + 2 * PADDING;
		totalHeight = itemHeight * recs.length;
		startY = (height - totalHeight) / 2;
	}

	private void render(Graphics g) {
		g.setColor(BGR_COLOR);
		g.fillRect(0, 0, width, height);
		
		ThemeManager.drawStringCenter(g, "HIGH SCORE", width, startY - 3,
				ThemeManager.BIG_STYLE,	Graphics.BOTTOM);
		
		int y = startY + PADDING;
		for (int i = 0; i < recs.length; i++) {
			if (i + 1 != blinkItem) {
				ThemeManager.drawStringCenter(g, recs[i].toString(i + 1),
						width, y, ThemeManager.SMALL_STYLE, Graphics.TOP);
			}
			y += itemHeight;
		}
		
		ThemeManager.drawStringCenter(g, "OK", width, height - 4,
				ThemeManager.BIG_STYLE, Graphics.BOTTOM);
	}
	
	private void backToMenu() {
		menuCanvas.resetForAnimate();
		display.setCurrent(menuCanvas);
	}
	
	private void backToBoard() {
		board.shape.a = Algorithms.createNewMatrix();
		board.shape.score = 0;
		board.setGameShape(board.shape);
		display.setCurrent(board);
	}
	
	private void back() {
		if (isBackToMenu) {
			backToMenu();
		} else {
			backToBoard();
			isBackToMenu = true;
		}
	}
	
	protected void pointerPressed(int x, int y) {
		back();
	}
	
	protected void keyPressed(int keyCode) {
		back();
	}

	public void run() {
		Graphics g = getGraphics();
		Thread t = Thread.currentThread();
		while (t == thread) {
			render(g);
			blinkItem = -blinkItem;
			flushGraphics();
			try {
				Thread.sleep(500);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	}
	
	public void stop() {
		thread = null;
		blinkItem = 100;
	}
	
	protected void showNotify() {
		thread = new Thread(this);
		thread.start();
	}
	
	protected void hideNotify() {
		stop();
	}
	
	public void insertScore(ScoreRecord sr) {
		int i;
		int len = recs.length;
		for (i = 0; i < len; i++) {
			if (sr.score > recs[i].score) {
				break;
			}
		}
		if (i == len) {
			return;
		}
		for (int j = len - 1; j > i; j--) {
			recs[j] = recs[j - 1];
		}
		recs[i] = sr;
		blinkItem = i + 1;
	}

}
